package com.sbm.datasource.config;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Component
@Configuration
@ConfigurationProperties(prefix = "spring")
public class DefaultDataSourceConfig {
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultDataSourceConfig.class);

    private Map<String, Object> datasource;

    private DataSourceConfig config;

    public void initConfig() {
        if (this.config == null) {
            try {
                this.config = new DataSourceConfig(datasource);
            } catch (Exception ex) {
                LOGGER.error("默认数据源：配置初始化失败——" + ex.getMessage());
                ex.printStackTrace();
            }
        }
    }

    public Map<String, Object> getDatasource() {
        return datasource;
    }

    public void setDatasource(Map<String, Object> datasource) {
        this.datasource = datasource;
    }

    public DataSourceConfig getDataSourceConfig() {
        initConfig();
        return this.config;
    }
}
